﻿Imports System.Data.OleDb

Public Class AddRecipe
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub tb_RestaurantAddress_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tb_CookTime.TextChanged

    End Sub

    Protected Sub btn_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Submit.Click
        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(fup_recipe.FileName)
        If fup_recipe.HasFile Then
            fup_recipe.SaveAs(Server.MapPath("pictures") & "/" & newFileName)
        End If

        Dim sql As String = "INSERT INTO UserRecipe (RecipeName,CookTime,Serves,Difficulty,Ingredients,CookingSteps,MichelinStars,AddedBy,RecipePicture) VALUES (@f1,@f2,@f3,f4,@f5,@f6,@f7,@f8,@f9)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", tb_RecipeName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_CookTime.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Serves.Text)
        cmd.Parameters.AddWithValue("@f4", tb_Difficulty.Text)
        cmd.Parameters.AddWithValue("@f5", tb_Ingredients.Text)
        cmd.Parameters.AddWithValue("@f6", tb_CookingSteps.Text)
        cmd.Parameters.AddWithValue("@f7", rbl_MichelinStars.SelectedValue)
        cmd.Parameters.AddWithValue("@f8", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f9", "pictures/" & newFileName)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("UserRecipe.aspx")

    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function


End Class